package com.wangjin.javaProject.service;

import com.alibaba.fastjson.JSONObject;
import com.wangjin.javaProject.Constants;
import com.wangjin.javaProject.request.HttpUtils;

import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

/**
 * @Creator: WangJin
 * @desc:
 * @Creation Data: 2021/12/26 20:40
 **/
public class GetBestBlockHash {
    String  auth = Constants.BITCOIN_RPC_USER + ":" + Constants.BITCOIN_RPC_PWD;
    //base64编码
    Base64.Encoder encoder = Base64.getEncoder();
    String authBase64 = encoder.encodeToString(auth.getBytes());
    //准备请求头
    public String getbestblockhash(){
        Map<String,String> header = new HashMap<>();
        header.put("ContentType","application/json");
        header.put("Authorization","Basic "+authBase64);

        //准备请求数据
        Map<String,Object> body = new HashMap<>();
        body.put("id",1001);
        body.put("method","getbestblockhash");
        body.put("params",null);
        body.put("json_rpc","2.0");
        byte[] bodyBytes = JSONObject.toJSONBytes(body);

        String result = HttpUtils.post(Constants.BITCOIN_RPC_URL, header,bodyBytes);
        return result;
    }
}
